#include"../head/client.h"
#include <crypt.h>
#include <math.h>
#include <mysql/mysql.h>
#include <shadow.h>

int interface(){
    printf("----------------欢迎使用山寨客户端--------------------------------\n");
    printf("1.cd(切换目录)------------------------------------------后接目录名\n");
    printf("2.ls(列出目录下文件)------------------------------------后啥都不接\n");
    printf("3.puts(上传文件)----------------------------------------后接文件名\n");
    printf("4.gets(下载文件)----------------------------------------后接文件名\n");
    printf("5.remove(删除文件)--------------------------------------后接文件名\n");
    printf("6.pwd(显示当前目录)---------------------------------------后接空气\n");
    printf("7.mkdir(创建目录)---------------------------------------后接目录名\n");
    printf("8.rmdir(删除目录)---------------------------------------后接目录名\n");
    //printf("9.login(退出登录)-------------------------------------------------\n");
    //printf("10.logout(登录退出)----- --------------------------------------------!\n");
    printf("11.resume(断点续传)----- --------------------------------------------!\n");
    printf("12.quit客户端退出----- --------------------------------------------!\n");
    //printf("13.rmdie(删除目录)----- --------------------------------------------!\n");
    return 0;
} 
int enter(int netfd){ //登录
    char name[MAX_PATH_LEN]={0};
    while(1){
        printf("请输入账户:");
        fflush(stdout);
        char buf[MAX_PATH_LEN]={0};
        scanf("%s",buf);
        //printf("%s\n",buf);
        strcpy(name,buf);
        //transmit(netfd,0,strlen(buf),buf);
        TLVMsg msg;
        MYSQL *mysql=mysql_init(NULL);
        mysql_database(mysql);

        int ret=mysql_sekect(mysql,"username",buf);
        //printf("ret = %d\n",ret);
        if(ret==0) {
            printf("账户不存在\n");
        }else{
            transmit(netfd,CMD_LOGIN,strlen(buf),buf);
            break;
        }
    }
    while(1){
        //transmit(netfd,CMD_LOGIN,strlen(name),name);
        struct crypt_data data;
        TLVMsg msg;
        //TLVMsg msg_2;
        char password[MAX_PATH_LEN]={0};
        printf("请输入密码:\n");
        scanf("%s",password);

        recvfd(netfd,&msg); //收盐值
        char salt[MAX_PATH_LEN]={0};
        //recvfd(netfd,&msg_2);  //收
        strcpy(salt,msg.value);
        //printf("%s\n",salt);
        char *pass=crypt_r(password,salt,&data);
        char kuf[MAX_PATH_LEN]={0};
        strncpy(kuf,pass,64);
        kuf[63]='\0';
        transmit(netfd,8,strlen(kuf),kuf);
        
        recvfd(netfd,&msg);
        if(msg.type==CMD_LOGIN){
            printf("%s\n",msg.value);
            interface();
            printf("登录成功，请输入指令\n");
            return 0;
        }else{
            printf("登录失败，请输入密码\n");
            printf("%s\n",msg.value);
            transmit(netfd,CMD_LOGIN,strlen(name),name);
            transmit(netfd,CMD_LOGIN,strlen(name),name);
        }
    }
        return 0;
}


int mysql_delete(MYSQL *mysql,char *name){ // 修改数据(注销)
     char query[256]={0};
     sprintf(query,"update user set tomb=1 where username='%s';\n",name);
     int qret=mysql_query(mysql,query);//把sql语句字符串发给服务端
     if(qret!=0){
         fprintf(stderr,"%s:%s\n","mysql_real_connect",mysql_error(mysql));
     }
     return 0;
}

int register_account(int netfd)  //注册
{   
    while(1){
        char handle[256]={0};
        char password[256]={0};
        printf("请输入一个用户:");
        fflush(stdout);
        scanf("%s",handle);
        fflush(stdout);
        //入表
        printf("请输入密码:");
        fflush(stdout);
        scanf("%s",password);
        int ret= clicent_sign_in(handle,password,netfd);
        //TLVMsg msg;
        printf("ret = %d\n",ret);
        if(ret!=0){
            printf("账户已存在,请重新注册\n");
        }
        else{
            //recvfd(netfd,&msg);
            interface();
            //printf("%s\n",msg.value);
            printf("请输入指令：\n");
            return 0;
        }
    }
    return 0;
}

int manage_accounts(){    //注销
    MYSQL *mysql=mysql_init(NULL);
    mysql_database(mysql);
    char name[MAX_PATH_LEN]={0};
    printf("请输入注销账户:\n");
    scanf("%s",name);
    mysql_delete(mysql,name);
    return 0;

}
